package com.whmnx.chitchat.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

import com.orhanobut.logger.Logger;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.tencent.qcloud.tim.uikit.component.video.CameraActivity;
import com.tencent.qcloud.tim.uikit.component.video.JCameraView;
import com.tencent.qcloud.tim.uikit.modules.chat.base.BaseInputFragment;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfoUtil;
import com.tencent.qcloud.tim.uikit.utils.TUIKitConstants;
import com.tencent.qcloud.tim.uikit.utils.TUIKitLog;
import com.tencent.qcloud.tim.uikit.utils.ToastUtil;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.bean.BaseBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.main.MainActivity;
import com.whmnx.chitchat.ui.chat.ChatActivity;
import com.whmnx.chitchat.ui.chat.model.UserModel;
import com.whmnx.chitchat.ui.my.MyInfoActivity;
import com.whmnx.chitchat.utils.PhotoUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.ChatLayout;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.app.Activity.RESULT_OK;
import static com.whmnx.chitchat.ui.my.MyInfoActivity.CODE_CAMERA_REQUEST;
import static com.whmnx.chitchat.ui.my.MyInfoActivity.CODE_GALLERY_REQUEST;
import static com.whmnx.chitchat.ui.my.MyInfoActivity.CODE_RESULT_REQUEST;
import static com.whmnx.chitchat.ui.my.MyInfoActivity.OUTPUT_X;
import static com.whmnx.chitchat.ui.my.MyInfoActivity.OUTPUT_Y;

/**
 * author : huang
 * date   : 2020/4/20 0020 14:29
 * version: 1.0
 *
 * @author Administrator
 */

@SuppressLint("ValidFragment")
public class CustomInputFragment extends BaseInputFragment {

    public static final int FILE_SELECT_CODE = 0;

    private static final int FILE_SELECTOR_CODE = 100;
    private ChatInfo mChatInfo;

    private ChatLayout mChatLayout;

    private List<UserModel> mContactList = new ArrayList<>();

    public Uri imageUri;
    private Uri newUri;
    private Uri cropImageUri;
    private Activity mActivity;
    public File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/photo.jpg");
    private File fileCropUri = new File(Environment.getExternalStorageDirectory().getPath() + "/crop_photo.jpg");

    @SuppressLint("ValidFragment")
    public CustomInputFragment(Activity mActivity) {
        this.mActivity = mActivity;

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View baseView = inflater.inflate(R.layout.test_chat_input_custom_fragment, container, false);
        TextView tv1 = baseView.findViewById(R.id.tv_1);
        TextView tv2 = baseView.findViewById(R.id.tv_2);
        TextView tv3 = baseView.findViewById(R.id.tv_3);

        tv1.setOnClickListener(v -> {
            showDialog();
        });

        tv2.setOnClickListener(v -> {
            try {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                //打开文本文件
                intent.setType("text/plain");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
              //  ChatActivity.getForegroundActivity().startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_SELECT_CODE);
                mActivity.startActivityForResult(intent, FILE_SELECT_CODE);
            } catch (android.content.ActivityNotFoundException ex) {
               UItils.showToastSafe("亲，木有文件管理器啊");
            }
        });

        tv3.setOnClickListener(v -> {
            //1对1 视频聊天
            UserModel userModel = new UserModel();
            userModel.userId = mChatInfo.getId();
            userModel.userName = mChatInfo.getChatName();
            mContactList.add(userModel);
            // TRTCVideoCallActivity.startCallSomeone(ChatActivity.getForegroundActivity(), mContactList);
            //互动直播
            //  Intent intent = new Intent(ChatActivity.getForegroundActivity(), RTCActivity.class);
            //  Log.d("hhh", String.valueOf(new Random().nextInt(9000)));
            //  intent.putExtra(Constants.ROOM_ID,String.valueOf(new Random().nextInt(9000)));
            //    intent.putExtra(Constants.USER_ID,SPUtil.getString(Constants.PHONE));
            //   ChatActivity.getForegroundActivity().startActivity(intent);
        });
        return baseView;
    }

    private void showDialog() {
        Dialog dialogHead = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_head_portrait, null);

        view.findViewById(R.id.dialog_shoot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  Intent captureIntent = new Intent(getActivity(), CameraActivity.class);
                //   captureIntent.putExtra(TUIKitConstants.CAMERA_TYPE, JCameraView.BUTTON_STATE_ONLY_CAPTURE);
                imageUri = Uri.fromFile(fileUri);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    imageUri = FileProvider.getUriForFile(getActivity(), "com.whmnx.chitchat.FileProvider", fileUri);
                }
                PhotoUtil.takePicture(getActivity(), imageUri, CODE_CAMERA_REQUEST);
                //   photoUtils.takePicture(MyUserInfoActivity.this);
                dialogHead.dismiss();
            }
        });
        view.findViewById(R.id.dialog_photo_album).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoUtil.openPic(getActivity(), CODE_GALLERY_REQUEST);
                // photoUtils.takePicture(MyUserInfoActivity.this);
                //  photoUtils.selectPicture(MyUserInfoActivity.this);
                dialogHead.dismiss();
            }
        });
        view.findViewById(R.id.dialog_cancel).setOnClickListener(v -> dialogHead.dismiss());
        dialogHead.setContentView(view);
        Window window = dialogHead.getWindow();
        if (window == null) {
            return;
        }
        dialogHead.show();//显示对话框//设置Dialog从窗体底部弹出
        window.setGravity(Gravity.BOTTOM);
        //获取窗体的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        // lp.y = 20;//设置Dialog距离底部的距离
        //将属性设置给窗体
        window.setAttributes(lp);
    }
}
